﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityCore;
using DataManager;

namespace MBBStoreClient
{
    public partial class frmProfileEntry : Form
    {
        ProfileManager profManager = new ProfileManager();
        ProfileInfo [] profileInfo = new ProfileInfo[2];
        ProfileInfo profile = new ProfileInfo();

        bool isNew = true;
        string num = "";

        public frmProfileEntry()
        {
            InitializeComponent();

            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);

            profManager.ProcessCompleted += new CoreHandler(profManager_ProcessCompleted);
            profManager.ProcessError += new CoreHandler(profManager_ProcessError);

            btnCommit.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            
            if (isNew == true)
            {
                if (txtCustomerName.Text != "" && cmbStatus.Text != "" && date.Text != "")
                {
                    for (int i = 0; i < 2; i++)
                    {
                        if (i == 0)
                            profileInfo[i].referenceNumber = txtSalesRefNum.Text;
                        else
                            profileInfo[i].referenceNumber = txtChargeRefNum.Text;

                        profileInfo[i].name = txtCustomerName.Text;
                        profileInfo[i].address = txtAddress.Text;
                        profileInfo[i].contactNumber = txtContactNumber.Text;
                        profileInfo[i].status = cmbStatus.Text;
                        profileInfo[i].date = DateTime.Parse(date.Text).Date;
                    }
                    profManager.AddProfile(profileInfo);
                }
            }
            else
            {
                profile.SalesReferenceNumber = txtSalesRefNum.Text;
                profile.ChargeReferenceNumber = txtChargeRefNum.Text;
                
                profile.name = txtCustomerName.Text;
                profile.address = txtAddress.Text;
                profile.contactNumber = txtContactNumber.Text;
                profile.status = cmbStatus.Text;
                profile.date = DateTime.Parse(date.Text);
                profManager.UpdateProfile(profile);
            }
            
        }

        void profManager_ProcessError(string pcode, string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                
                if (pcode == "GetCancelledRef")
                {
                    profManager.AutoRef();
                }
                else
                {
                    MessageBox.Show(this, msg, "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            });
        }

        void profManager_ProcessCompleted(string pcode, string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                if (pcode == "AutoRef")
                {
                    txtSalesRefNum.Text = profManager.SalesReferenceNumber;
                    txtChargeRefNum.Text = profManager.ChargeReferenceNumber;
                    num = profManager.count;

                    txtCustomerName.Focus();
                }
                if (pcode == "AddProfile")
                {
                    MessageBox.Show(this, "Profile successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    pnlForm.Dispose();
                    this.Dispose();
                }
                if (pcode == "UpdateProfile")
                {
                    MessageBox.Show(this, "Profile successfully updated.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    btnClose_Click(null, null);
                }

                if (pcode == "GetCancelledRef")
                {
                    txtSalesRefNum.Text = profManager.SalesReferenceNumber;
                    txtChargeRefNum.Text = profManager.ChargeReferenceNumber;
                    num = profManager.count;

                    txtCustomerName.Focus();
                }

                if (pcode == "SaveCancelledRef")
                {
                    pnlForm.Dispose();
                    this.Dispose();
                }
            });
        }
        
        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }

        void btnClose_Click(object sender, EventArgs e)
        {
            if (isNew)
            {
                DialogResult dlg = MessageBox.Show("This will cancel the current reference number. Are you sure?", "Cancel Reference Number", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (dlg == DialogResult.Yes)
                {
                    profManager.SaveCancelledRef(num);
                }
            }
            else
            {
                pnlForm.Dispose();
                this.Dispose();
            }
        }

        public Panel PanelForm
        {
            get { return pnlForm; }
        }

        public void Initialize(string Title, bool New)
        {
            isNew = New;
            this.Select();
            lblText.Text = Title;
            if (isNew == true)
            {
                profManager.GetCancelledRef();
            }
            else
            {
                btnCommit.Text = "Update";
            }
        }

        public void OldInfo(ProfileInfo oldProfile)
        {
            txtChargeRefNum.Text = oldProfile.ChargeReferenceNumber;
            txtSalesRefNum.Text = oldProfile.SalesReferenceNumber;
            txtCustomerName.Text = oldProfile.name;
            txtAddress.Text = oldProfile.address;
            txtContactNumber.Text = oldProfile.contactNumber;
            profile.uniqueName = oldProfile.name;
            date.Text = oldProfile.date.ToShortDateString();
            cmbStatus.Text = oldProfile.status;
            txtCustomerName.Enabled = false;
        }
    }
}
